package com.csiec.clock.adpter;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;

import com.csiec.clock.R;
import com.csiec.clock.activity.daka.JoinClockInfoActivity;
import com.csiec.clock.bean.myJoinClock.JoinClock;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;

import java.util.List;

public class MyJoinClockAdapter extends RecyclerView.Adapter<MyJoinClockAdapter.ViewHolder> {
    private final String TAG = MyJoinClockAdapter.class.getSimpleName();

    private final List<JoinClock> allJoinClock;
    private final Activity activity;

    private final int[] bgList = {R.drawable.status_red, R.drawable.status_green, R.drawable.status_yellow};
    private final String[] textList = {"进行中", "已结束", "未开始"};

    public MyJoinClockAdapter(List<JoinClock> allJoinClock, Activity activity) {
        this.allJoinClock = allJoinClock;
        this.activity = activity;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View itemView;
        itemView = layoutInflater.inflate(R.layout.cell_card_jion_clock, parent, false);
        return new MyJoinClockAdapter.ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        JoinClock clock = allJoinClock.get(position);
        holder.join_clock_num.setText(String.valueOf(position + 1));
        holder.join_clock_name.setText(clock.getName());
        holder.join_clock_time.setText(clock.getSingleTime());
        holder.join_clock_state.setBackgroundResource(bgList[clock.getState()]);
        holder.join_clock_state.setText(textList[clock.getState()]);

        holder.constraintLayout.setOnClickListener(v -> {
            //打卡结束，不能点进去
            if (clock.getState() == 1 || clock.getState() == 2) {
                QMUITipDialog tipDialog = new QMUITipDialog.Builder(activity)
                        .setIconType(QMUITipDialog.Builder.ICON_TYPE_INFO)
                        .setTipWord("打卡已结束")
                        .create();
                tipDialog.show();
                holder.join_clock_state.postDelayed(tipDialog::dismiss, 2000);
            } else {
                //跳转打卡详情页面
                JoinClock cardSend = allJoinClock.get(position);
                Intent intent = new Intent(activity.getApplication(), JoinClockInfoActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra("join_card", cardSend);
                activity.getApplication().startActivity(intent);
            }
        });
    }


    @Override
    public int getItemCount() {
        return allJoinClock.size();
    }


    static class ViewHolder extends RecyclerView.ViewHolder {

        TextView join_clock_num, join_clock_name, join_clock_state, join_clock_time;
        ConstraintLayout constraintLayout;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            join_clock_num = itemView.findViewById(R.id.join_clock_num);
            join_clock_name = itemView.findViewById(R.id.join_clock_name);
            join_clock_state = itemView.findViewById(R.id.join_clock_state);
            join_clock_time = itemView.findViewById(R.id.join_clock_time);
            constraintLayout = (ConstraintLayout) itemView.findViewById(R.id.join_clock_card_item);
        }
    }
}
